<?php

class Connection_checks extends Public_Controller {

	function __construct()
	{
		parent::__construct();
		$this->sSkinFile = 'ajax';
	}
	
	public function check_connection()
	{
		$iUserId = $this->user->userId === false ? 0 : $this->user->userId;
		
		$this->aData['json_encode'] = array('iUserId' => $iUserId);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	public function login_on_the_fly()
	{
		$qUser = $this->check_credentials();
		
		$iUserId = 0;
		
		if($qUser !== false)
		{
			$iUserId = $qUser->row(0)->user_id;
			
			$this->session->set_userdata('userId', $qUser->row(0)->user_id);
			$this->session->set_userdata('userName', $qUser->row(0)->user_fname);
			$this->session->set_userdata('userEmail', $qUser->row(0)->user_email);
			$this->session->set_userdata('userDatetime', $qUser->row(0)->user_datetime);
		}
		
		$this->aData['json_encode'] = array('iUserId' => $iUserId);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	public function reload_iframe_blank()
	{
		
	}
	
	
	
	private function check_credentials()
	{
		$this->load->model('db/users', 'users');
		
		// firstly check whether someone is trying to login with their username and password
		$this->users->user_login = $this->input->post('login_username', true, '');
		$this->users->user_passwd = md5($this->input->post('login_password', true, ''));
				
		$qUser = $this->users->get(array(), 1);
		
				
		if ($qUser->num_rows() === 1)
		{
			return $qUser;
		}
		
		// then check whether their using their email and password
		$this->users->user_email = $this->users->user_login;
		$this->users->user_login = false;

		$qUser = $this->users->get(array(), 1);
				
		if ($qUser->num_rows() === 1)
		{
			return $qUser;
		}

		// if we've not got a user, return null;
		return false;

	}
}


?>